package com.fzc.rhinooffice.module.workbench;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.fzc.rhinooffice.R;
import com.fzc.rhinooffice.common.RemoteInvoke;
import com.fzc.rhinooffice.common.utils.DateUtil;
import com.fzc.rhinooffice.common.utils.StringUtil;
import com.fzc.rhinooffice.module.BaseActivity;
import com.fzc.rhinooffice.module.CommonAdapter;
import com.fzc.rhinooffice.module.ViewHolder;
import com.fzc.rhinooffice.module.entity.Sms;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cong on 15/5/18.
 */
@ContentView(R.layout.activity_sms_detail)
public class SmsDetailActivity extends BaseActivity implements OnItemClickListener {


    @ViewInject(R.id.tv_title)
    private TextView tv_title;

    @ViewInject(R.id.list_sms)
    public ListView list_sms;

    @ViewInject(R.id.edt_sms_content)
    private EditText edt_sms_content;

    @ViewInject(R.id.srl_refresh_view)
    private SwipeRefreshLayout srl_refresh_view;

    private CommonAdapter<Sms> commonAdapter;

    private List<Sms> smsList;

    public Sms sms;

    private int page = 1;
    private String content;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        initUI();

    }

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    JSONObject jsonObject = (JSONObject) msg.obj;
                    LogUtils.e(">>>>>>>>>>>" + jsonObject.toString());


                    break;

                case -1:
                    StringUtil.showToast("未知错误");

                    break;
            }
        }
    };

    @OnClick(R.id.btn_send)
    private void send(View v) {

        smsAdd();
    }

    private void smsAdd() {
        content = edt_sms_content.getText().toString();
        if (StringUtil.isEmpty(content)) {
            StringUtil.showToast("请输入发送的内容！");
            return;
        }
        //加进去
        Sms sms1 = new Sms();
        sms1.from_name = "我";
        sms1.content = content;
        sms1.send_time = DateUtil.getDataByY_M_D();
        smsList.add(sms1);
        RemoteInvoke.smsAdd(mHandler, sms1.send_time, sms.from_id, sms1.send_time, smsList.size());
        edt_sms_content.setText("");
        initAdapter();
    }

    private void initUI() {

        tv_title.setText("内部短信");
        list_sms.setOnItemClickListener(this);

        srl_refresh_view.setColorSchemeResources(R.color.swipe_color_1, R.color.swipe_color_2, R.color.swipe_color_3, R.color.swipe_color_4);
        srl_refresh_view.setSize(SwipeRefreshLayout.LARGE);
        srl_refresh_view.setProgressViewEndTarget(true, 100);

        sms = (Sms) getIntent().getSerializableExtra("sms");
        smsList = new ArrayList<Sms>();
        smsList.add(sms);

        initAdapter();


    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }


    private void initAdapter() {
        if (commonAdapter == null) {
            commonAdapter = new CommonAdapter<Sms>(this, smsList, R.layout.sms_left_item, R.layout.sms_right_item, "我") {

                @Override
                public void convert(ViewHolder holder, Sms item) {
                    holder.setText(R.id.tv_sms_detail_avatar, item.from_name);
                    holder.setText(R.id.tv_sendtime, item.send_time);
                    holder.setText(R.id.tv_chatcontent, item.content);

                }
            };
            list_sms.setAdapter(commonAdapter);

        } else {
            LogUtils.e("刷新");
            commonAdapter.notifyDataSetChanged();
        }
    }


    @OnClick({R.id.iv_back, R.id.tv_title})
    private void OpenDL(View v) {
        finish();
    }
}